/**
 * Created by lintao_alex on 2017/9/21.
 */


namespace game{
    import DisplayObject = egret.DisplayObject;
    import Shape = egret.Shape;
    import GObject = fairygui.GObject;
    import GComponent = fairygui.GComponent;
    import EgretArmatureDisplay = dragonBones.EgretArmatureDisplay;
    import EgretFactory = dragonBones.EgretFactory;

    export class DisplayUtils{
        public static removeFromParent(display:DisplayObject){
            let parent = display.parent
            if(parent){
                parent.removeChild(display);
            }
        }

        public static getDebugCirc(radius:number=5, color:number=0xff):Shape{
            let out = new Shape();
            out.graphics.beginFill(color)
            out.graphics.drawCircle(0, 0, radius);
            out.graphics.endFill();

            return out;
        }

        static topChild(child:DisplayObject){
            let parent = child.parent
            if(parent){
                parent.setChildIndex(child, parent.numChildren - 1);
            }
        }

        static getCenterY(view:DisplayObject|GObject){
            let size = view instanceof GComponent ? view.viewHeight : view.height;
            return Math.round(view.y + 0.5 * size);
        }
        static getCenterX(view:DisplayObject|GObject){
            let size = view instanceof GComponent ? view.viewWidth : view.width;
            return Math.round(view.x + 0.5 * size);
        }
        static adaptSize(view:DisplayObject, target:GComponent){
            view.scaleX = target.viewWidth/view.width;
            view.scaleY = target.viewHeight/view.height;
        }

        static addToWorldClock(display:EgretArmatureDisplay){
            if(!display || !display.armature)return;
            EgretFactory.clock.add(display.armature);
        }

        static removeFromWorldClock(display:EgretArmatureDisplay){
            if(!display || !display.armature)return;
            EgretFactory.clock.remove(display.armature);
        }
    }
}